package com.buddy.summary.code.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TextUtil {

    /**
     * 写入到文件
     *  如需换行，请自行拼接 \n  换行符
     * @param filePath 文件路径，注：不是文件夹
     * @param content
     */
    public static void writToText(String filePath, String content) throws Exception {
        BufferedWriter writer = null;
        try {
            String path = filePath.substring(0, filePath.lastIndexOf("/"));
            File directory = new File(path);
            if (!directory.exists() || !directory.isDirectory()) {
                directory.mkdirs();
            }
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new BufferedWriter(new FileWriter(file, true));
//            writer.write("\n" + content);//有换行
            writer.write(content);//不要换行
        } catch (Exception e) {
            throw new Exception("写入文件异常", e);
        } finally {
            if (CommonUtil.isNotEmpty(writer)) {
                writer.close();
            }
        }
    }

    /**
     * 读取文件，返回数组对象
     *
     * @param fileName 文件路径，注：不是文件夹
     * @return
     * @throws Exception
     */
    public static Object[] readToArray(String fileName) throws Exception {
        String encoding = "utf-8"; // 字符编码(可解决中文乱码问题 )
        File file = new File(fileName);
        if (file.isFile() && file.exists()) {
            List list = new ArrayList();
            InputStreamReader read = null;
            BufferedReader bufferedReader = null;
            try {
                read = new InputStreamReader(new FileInputStream(file), encoding);
                bufferedReader =  new BufferedReader(read);
                String lineTXT = null;
                while ((lineTXT = bufferedReader.readLine()) != null) {
                    list.add(lineTXT);
                }
                return list.toArray();
            } catch (Exception e) {
                throw new Exception("读取txt文件异常");
            } finally {
                if (CommonUtil.isNotEmpty(read)){
                    read.close();
                }
                if (CommonUtil.isNotEmpty(bufferedReader)){
                    bufferedReader.close();
                }
            }

        } else {
            throw new Exception("找不到文件路径或者路径是一个目录");
        }
    }

    /**
     * 读取文件，返回数组对象
     *
     * @param file 文件流
     * @return
     * @throws Exception
     */
    public static Object[] readTxtFileToArray(File file) throws Exception {
        String fileName = file.getName();
        String fileType = fileName.substring(fileName.lastIndexOf("." + 1));
        if ("txt".endsWith(fileType)) {
            throw new Exception("文件类型不是txt");
        }
        String encoding = "utf-8"; // 字符编码(可解决中文乱码问题 )
        List list = new ArrayList();
        InputStreamReader read = null;
        BufferedReader bufferedReader = null;
        try {
            read = new InputStreamReader(new FileInputStream(file), encoding);
            bufferedReader = new BufferedReader(read);
            String lineTXT = null;
            while ((lineTXT = bufferedReader.readLine()) != null) {
                list.add(lineTXT);
            }
            read.close();
        } catch (Exception e) {
            throw new Exception("读取txt文件异常", e);
        }finally {
            if (CommonUtil.isNotEmpty(read)){
                read.close();
            }
            if (CommonUtil.isNotEmpty(bufferedReader)){
                bufferedReader.close();
            }
        }

        return list.toArray();
    }
}
